package com.car.repairCarTest;

import com.car.entity.Repair;
import com.car.entity.User;
import com.car.service.repair.impl.RepairServiceImpl;
import com.car.util.PageUtil;
import lombok.experimental.StandardException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RepairTest1 {

    @Autowired
    RepairServiceImpl repairServiceImpl;

    @Test
    public void testGetRepairByIdQuery(){
        Repair r = repairServiceImpl.getRepairByIdQuery(2);
        System.out.println(r.getCarIdCard()+"'"+r.getUser().getUserName());
    }

    @Test
    public void testGetAllRepairQuery(){
        Repair rr = new Repair();
        String carIdCard = "豫A";
        Map<String,Object> map = new HashMap<>();
        map.put("carIdCard","豫B");
        map.put("userName","zhangsan");
        List<Repair> repairList = repairServiceImpl.getAllRepairQuery(map);
        for (Repair r : repairList) {
            System.out.println(r.getCarIdCard()+"==============");
        }
    }

    @Test
    public void testAddRepairInsert(){
        Repair r = new Repair();
        r.setCarIdCard("豫V22222");
        r.setInitMile(5000);
        r.setInitOil(20);
        r.setMobile("15067896789");
        r.setMoney(500);
        r.setRemark("无");
        r.setRepairContent("补漆");
        r.setRepairEnd("2021-11-20");
        r.setRepairPlace("郑州五菱宏光维修厂");
        r.setRepairStart("2021-11-12");
        r.setReturnMile(5000);
        r.setReturnOil(20);
        User u = new User();
        u.setUserId(2);
        r.setUser(u);
        repairServiceImpl.addRepairInsert(r);

    }

    @Test
    public void testEditRepairUpdate(){
        Repair r = new Repair();
        r.setRepairId(7);
        r.setCarIdCard("豫V33333");
        r.setInitMile(5000);
        r.setInitOil(20);
        r.setMobile("15067896789");
        r.setMoney(500);
        r.setRemark("无");
        r.setRepairContent("补漆");
        r.setRepairEnd("2021-11-20");
        r.setRepairPlace("郑州五菱宏光维修厂");
        r.setRepairStart("2021-11-12");
        r.setReturnMile(5000);
        r.setReturnOil(20);
        User u = new User();
        u.setUserId(2);
        r.setUser(u);
        repairServiceImpl.editRepairUpdate(r);

    }


    @Test
    public void testDelRepairByIdDelete(){
        Repair r = new Repair();
        r.setRepairId(7);
        repairServiceImpl.delRepairByIdDelete(r);

    }

    @Test
    public void pageTest1(){
        Repair r = new Repair();
        r.setCarIdCard("");
        User u = new User();
        u.setUserName("zhang");
        r.setUser(u);
        Map<String,Object> map =new HashMap<>();
        map.put("repair",r);
        int pageSize = 3;
        int pageNum = 3;
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        int count =  repairServiceImpl.getRepairCountQuery(map);
        System.out.println(count);
        int total = PageUtil.getTotalPage(count,pageSize);
        //将总页数添加到map中
        map.put("total",total);
        //通过页码和条数算出本次分页的起始位置
        int selectstart = pageNum*(pageSize-1);
        //通过页码和条数算出本次分页的结束位置
        int selectEnd =pageNum*pageSize;
        //将起始位置封装到map中，方便后续dao层调用
        map.put("selectstart",1);
        //将结束位置封装到map中，方便后续dao层调用
        map.put("selectEnd",selectEnd);
        //通过map包含的条件 查询符合的repair组成一个List返回
        //条件包括： repair.carIdCard  repair.user.userName  模糊搜索  和  分页
        List<Repair> repairList = repairServiceImpl.getAllRepairQuery(map);
        for (Repair rr : repairList) {
            System.out.println(rr.getCarIdCard()+"=============="+rr.getUser().getPhone());
        }

    }



}
